import { SceneMode } from 'cesium/Cesium';
import NavigationControl from './NavigationControl';

/**
 * 切换SceneMode
 *
 * @alias SceneModeNavigationControl
 * @constructor
 * @abstract
 *
 * @param {Terria} terria The Terria instance.
 */
const SceneModeNavigationControl = function (terria) {
  NavigationControl.apply(this, arguments);

  /**
   * Gets or sets the name of the control which is set as the control's title.
   * This property is observable.
   * @type {String}
   */
  this.name = arguments[2] === '3D' ? '切换为二维视图' : '切换为三维视图';
  this.text = arguments[2] === '3D' ? '2D' : '3D';

  /**
   * Gets or sets the CSS class of the control. This property is observable.
   * @type {String}
   */
  this.cssClass = 'navigation-control-icon-scenemode';

  this.setSceneMode = terria.options.setSceneMode;
};

SceneModeNavigationControl.prototype = Object.create(NavigationControl.prototype);

SceneModeNavigationControl.prototype.changeSceneMode = function () {
  this.isActive = true;
  const { scene } = this.terria;
  if (scene.mode === SceneMode.SCENE3D) {
    scene.mode = SceneMode.SCENE2D;
    this.name = '切换为三维视图';
    this.text = '3D';
    // 设置全局视图模式
    if (this.terria.options.setSceneMode) {
      this.terria.options.setSceneMode('2D');
    }
  } else {
    scene.mode = SceneMode.SCENE3D;
    this.name = '切换为二维视图';
    this.text = '2D';
    // 设置全局视图模式
    if (this.terria.options.setSceneMode) {
      this.terria.options.setSceneMode('3D');
    }
  }
  this.isActive = false;
};

/**
 * When implemented in a derived class, performs an action when the user clicks
 * on this control
 * @abstract
 * @protected
 */
SceneModeNavigationControl.prototype.activate = function () {
  this.changeSceneMode();
};

export default SceneModeNavigationControl;
